$(function(){
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'User/getRepeatLogin');
            this.dataGrid.parseToolbar().init().activeBtn(null, $('#bt_list_del, #bt_list_enable, #bt_list_disable'));

            this.listener();
        },
        listener: function () {
            var $this = this;
            xhExtra.clearLoadSuccess(this.dataGrid);

            //查找
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function (){
                    $this.funObj().search();
                })
            );
            //增加
            $('#bt_list_add').click(function() {
                $this.funObj().addRepeat();
            });
            //启用
            $('#bt_list_enable').click(function(){
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().setStatus('enable', '确认启用？');
            });
            //禁用
            $('#bt_list_disable').click(function(){
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().setStatus('disable', '确认禁用？');
            });
            //删除
            $('#bt_list_del').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().delRepeatLogin();
            });
        },
        funObj: function(){
            var $this = this;
            return {
                search: function () {
                    $this.dataGrid.reload({
                        logonName: xhExtra.getValue($('#list_logonName')),
                        school: xhExtra.getValue($('#list_school')),
                        startTime: xhExtra.getValue($('#list_startTime')),
                        endTime: xhExtra.getValue($('#list_endTime'))
                    })
                },
                addRepeat: function () {
                    window.top.swal({
                        title: '新增重复登陆',
                        confirmButtonText: '确认',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        html: '<textarea id="logon_names" class="swal2-input" style="height: 60px" placeholder="输入用户名，多个用户用逗号隔开..." ></textarea>' +
                        '<select id="time_limit" class="swal2-select">' +
                        '<option value="1">一天</option>' +
                        '<option value="2">一周</option>' +
                        '<option value="3">一月</option>' +
                        '<option value="4">永久</option>' +
                        '</select>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var logonNames  = $('#logon_names', window.top.document).val(),
                                    timeLimit   = $('#time_limit', window.top.document).val();
                                if (!logonNames){
                                    reject('登陆名不能为空！');
                                }
                                else {
                                    xhExtra.ajaxRequest('User/createRepeatLogin',
                                        {logonNames: logonNames, timeLimit: timeLimit},
                                        {success: true, table: $this.dataGrid}
                                    );
                                }
                            })
                        }
                    });
                },
                setStatus: function(target, msg) {
                    xhExtra.askChangeStatus('User/setRepeatLoginState', {userId: $this.dataGrid.getSelectValue('userId'), target: target}, $this.dataGrid, msg);
                },
                delRepeatLogin: function () {
                    xhExtra.askChangeStatus('User/delRepeatLogin', {userId: $this.dataGrid.getSelectValue('userId')}, $this.dataGrid, '确认删除？');
                }
            }
        }
    }
};